package com.ruoyi.web.controller.projects;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.system.response.ProjectsInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.ProjectsProgress;
import com.ruoyi.system.service.IProjectsProgressService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 项目进度描述Controller
 *
 * @author tanshen
 * @date 2023-10-12
 */
@Controller
@RequestMapping("/projects/progress")
public class ProjectsProgressController extends BaseController {
    private String prefix = "projects/progress";

    @Autowired
    private IProjectsProgressService projectsProgressService;

    @RequiresPermissions("projects:progress:view")
    @GetMapping()
    public String progress() {
        return prefix + "/progress";
    }

    /**
     * 查询项目进度描述列表
     */
    @RequiresPermissions("projects:progress:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ProjectsProgress projectsProgress) {
        startPage();
        List<ProjectsProgress> list = projectsProgressService.selectProjectsProgressList(projectsProgress);
        return getDataTable(list);
    }

    /**
     * 导出项目进度描述列表
     */
    @RequiresPermissions("projects:progress:export")
    @Log(title = "项目进度描述", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ProjectsProgress projectsProgress) {
        List<ProjectsProgress> list = projectsProgressService.selectProjectsProgressList(projectsProgress);
        ExcelUtil<ProjectsProgress> util = new ExcelUtil<>(ProjectsProgress.class);
        return util.exportExcel(list, "项目进度描述数据");
    }

    /**
     * 新增项目进度描述
     */
    @GetMapping("/add/{id}")
    public String add(@PathVariable("id") Long id, ModelMap mmap) {
        mmap.put("projectId", id);
        ProjectsInfo projectsInfo = projectsProgressService.selectProjectsProgressByProjectId(id, new ProjectsInfo());
        mmap.put("progressList", projectsInfo.getProgressList());
        mmap.put("technicalAgreementProgressList", projectsInfo.getTechnicalAgreementProgressList());
        return prefix + "/add";
    }

    /**
     * 新增保存项目进度描述
     */
    @RequiresPermissions("projects:progress:add")
    @Log(title = "项目进度描述", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ProjectsProgress projectsProgress) {
        projectsProgress.setCreateTime(DateUtils.getNowDate());
        projectsProgress.setUpdateTime(DateUtils.getNowDate());
        projectsProgress.setCreateBy(ShiroUtils.getLoginName());
        String progressContent = projectsProgress.getProgressContent();
        String technicalAgreementProgress = projectsProgress.getTechnicalAgreementProgress();
        if (StringUtils.isEmpty(progressContent) && StringUtils.isEmpty(technicalAgreementProgress)) {
            throw new ServiceException("请输入项目进度描述");
        }
        List<ProjectsProgress> list = new ArrayList<>();
        if (StringUtils.isNotEmpty(progressContent)) {
            ProjectsProgress pp = BeanUtils.copy(projectsProgress,ProjectsProgress.class);
            pp.setType(0);
            list.add(pp);
        }
        if (StringUtils.isNotEmpty(technicalAgreementProgress)) {
            ProjectsProgress pp = BeanUtils.copy(projectsProgress,ProjectsProgress.class);
            pp.setProgressContent(technicalAgreementProgress);
            pp.setType(1);
            list.add(pp);
        }
        return toAjax(projectsProgressService.saveBatch(list));
    }

    /**
     * 修改项目进度描述
     */
    @RequiresPermissions("projects:progress:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        ProjectsProgress projectsProgress = projectsProgressService.selectProjectsProgressById(id);
        mmap.put("projectsProgress", projectsProgress);
        return prefix + "/edit";
    }

    /**
     * 修改保存项目进度描述
     */
    @RequiresPermissions("projects:progress:edit")
    @Log(title = "项目进度描述", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ProjectsProgress projectsProgress) {
        return toAjax(projectsProgressService.updateProjectsProgress(projectsProgress));
    }

    /**
     * 删除项目进度描述
     */
    @RequiresPermissions("projects:progress:remove")
    @Log(title = "项目进度描述", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(projectsProgressService.deleteProjectsProgressByIds(ids));
    }
}
